<?php
 require_once("../inc/keywords.php");
 
  //Confirms that the POST Exist and if it's going to create or update an entry
 if(isset($_POST['createNew']))
 {
	//Creates new Keyword
 	if($_POST['createNew'] === '1')
 	{
 		print_r($_POST);
 		$results = addKeyword($_POST);
	
		print_r($results);
		
		if($results === true)
		{
			header("location:index.php?inserted");
		}
		else
		{
			$keyword = html_input_treatment($_POST);
		}
		
		$update = 1;
		$type_update = "New Keyword";
	}
	
	//Updates existing Keyword
	if($_POST['createNew'] === '0')
 	{
 		print_r($_POST);
 		$results = editKeyword($_POST);
	
		if($results === true)
		{
			header("location:index.php?updated");
		}
		else
		{
			$keyword = html_input_treatment($_POST);
		}
		
		$update = 0;
		$heading = "Updated";
		$type_update = "Update Keyword";

	}
 	
 }
 //Obtains Keyword information for editing
 elseif(isset($_GET['id']))
 {
 	
 	$keyword = getKeyword($_GET['id']);
	$keyword = html_input_treatment($keyword);

	
	$update = 0;
	$heading = "Updated";
	$type_update = "Update Keyword";
 }
 //Creates a blank Keyword for creating a new Keyword entry
 else 
 {
 	$keyword = blank_keyword();
	
	$update = 1;
	$heading = "New";
	$type_update = "New Keyword";
 }
  
?>
<!DOCTYPE html>
    <html>
    <head>
        <meta charset="utf-8" />
        <title><?=$title ?> <?=$type_update ?></title>
         <? require('../inc/header-info.php'); ?>
    </head>
    <body>
        <div class="container">
            <? include('../inc/nav.php'); ?>
         <form id="newKeyword" method="post" action="addkeyword.php">
         	<h4>Enter <?=$heading ?> Keyword</h4>
            <div class="input-append">
            	<input type="hidden" name="createNew" value='<?=$update?>'>
            	<input type="hidden" name="id" value="<?=$keyword['id']?>">
            	
  				<input class="span2 required" id="appendedInputButtons" size="16" type="text" name="value" placeholder="Keyword" value="<?=$keyword['value']?>">
  				<button class="btn" type="submit"><?=$heading?></button>
  				<a class="btn" href="<?=$url?>keywords/index.php">Cancel</a>
			</div>
			</form>   
		</div>
		
	<script type="text/javascript" src="http://jzaefferer.github.com/jquery-validation/jquery.validate.js"></script>	
	<script>
		$(function(){
			
			$("#newKeyword").validate();
			
		});
		
		
	</script>
	</body>
</html>